package com.framework2.utils;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.asiainfo.hybridapp.configs.ConstantsME;
import com.framework.security.AesUtils;
import com.framework.security.Base64Coder;
import com.framework.utils.PreferencesHelper;

/**
 * 加密string
 *
 * @author YobertJomi
 * className EncStringUtil
 * created at  2017/7/4  18:03
 */
public class EncStringUtil {
    private static volatile EncStringUtil singleton;

    private EncStringUtil() {
    }

    public static EncStringUtil getInstance() {
        if (singleton == null) {
            synchronized (EncStringUtil.class) {
                if (singleton == null) {
                    singleton = new EncStringUtil();
                }
            }
        }
        return singleton;
    }

    /**
     * @param context context
     * @param preName preName sharePreference的 key
     * @return
     */
    public String getString(Context context, @NonNull String preName) {
        return decodeString(context, PreferencesHelper.getInstance().getStringData(preName));
    }

    public void putString(Context context, String str, @NonNull String preName) {
        if (!TextUtils.isEmpty(str)) {
            if (TextUtils.isEmpty(PreferencesHelper.getInstance().getStringData(ConstantsME.aesKey))) {
                PreferencesHelper.getInstance().putInfo(ConstantsME.aesKey,
                        Base64Coder.encodeString(AesUtils.getInstance().generateKey()));
            }
            if (str.length() > 128) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i * 128 < str.length(); i++) {
                    String cookiesWithRSA = RSAmethodInRaw.rsaEncrypt(context, str.substring(i * 128,
                            Math.min((i + 1) * 128, str.length())));
                    if (i > 0)
                        sb.append(",,,,");
                    sb.append(Base64Coder.encodeString(cookiesWithRSA));
                }
                PreferencesHelper.getInstance().putInfo(preName,
                        AesUtils.getInstance().encrypt(Base64Coder.decodeString(PreferencesHelper.getInstance().getStringData(ConstantsME.aesKey)), sb.toString()));
            } else {
                String cookiesWithRSA = RSAmethodInRaw.rsaEncrypt(context, str);
                PreferencesHelper.getInstance().putInfo(preName,
                        AesUtils.getInstance().encrypt(Base64Coder.decodeString(PreferencesHelper.getInstance().getStringData(ConstantsME.aesKey)), Base64Coder.encodeString(cookiesWithRSA)));
            }
        }
    }

    private String decodeString(Context context, String cookies) {
        if (!TextUtils.isEmpty(cookies)) {
            String tm =
                    AesUtils.getInstance().decrypt(Base64Coder.decodeString(PreferencesHelper.getInstance().getStringData(ConstantsME.aesKey)), cookies);
            if (!TextUtils.isEmpty(tm)) {
                String cookie[] = tm.split(",,,,");
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < cookie.length; i++) {
                    String cookiesBase64decode = Base64Coder.decodeString(cookie[i]);
                    if (!TextUtils.isEmpty(cookiesBase64decode)) {
                        sb.append(RSAmethodInRaw.rsaDecrypt(context, cookiesBase64decode));
                    }
                }
                return sb.toString();
            }
        }
        return null;
    }
}
